/**
 * 
 */
package com.uminho.objects;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.MalformedURLException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.uminho.egendme.R;
import com.uminho.http.Webservice;
import com.uminho.tools.ApplicationContext;

import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;

/****c* Objects/SimpleService
* DESCRIPTION
* Objeto que guarda apenas os dados mais importantes de um serviço registado no egend.me. É particularmente importante para ser utilizada nas pesquisas para não utilizar demasiados recursos pois apenas guarda o identificador, nome, descrição e o avatar.
****
*/
public class SimpleService implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Boolean isPartner;
	Long id,category_id;
	String title, description;
	Drawable avatar; // http://stackoverflow.com/questions/4223472/how-to-display-image-from-internet-in-android

	public SimpleService(String id, String title, String description, Long category_id) {
		if (id != null)
			this.id = Long.parseLong(id);
		this.title = title;
		this.description = description;
		this.category_id = category_id;
		isPartner = false;
	}

	public SimpleService(SimpleService s) {
		this.id = s.getServiceId();
		this.title = s.getTitle();
		this.description = s.getDescription();
		this.category_id = s.getCategory_id();
		this.avatar=s.avatar;
		isPartner = false;
	}

	public SimpleService() {
		isPartner = false;
	}

	public Long getServiceId() {
		return id;
	}

	public void setServiceId(Long id) {
		this.id = id;
	}

	public void setServiceId(String id) {
		if (id != null)
			this.id = Long.parseLong(id);
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Long getCategory_id() {
		return category_id;
	}

	public void setCategory_id(Long category_id) {
		this.category_id = category_id;
	}

	public Drawable getAvatar() {
		return avatar;
	}

	public void setAvatar(Drawable avatar) {
		this.avatar = avatar;
	}

	
	
	
	public Boolean getIsPartner() {
		return isPartner;
	}

	public void setIsPartner(Boolean isPartner) {
		this.isPartner = isPartner;
	}

	/****f* SimpleService/setAvatar
	* DESCRIPTION 
	* Define um avatar do serviço. Esse avatar é uma imagem hospedada no servidor do Egend.me, por isso este método não recebe nenhum argumento.
	* RESULT 
	* É definido o avatar para o objeto associado e pronto a ser utilizado pela aplicação Android
	* EXAMPLE
	SimpleService s = new SimpleService(312321,"Cabelos", "Maior cabeleireiro de Braga");
	s.setAvatar();
	****
	*/
	public void setAvatar() throws MalformedURLException, IOException {
		String urlAvatar = Webservice.domain + "/avatars/service?id=" + this.getServiceId().toString() + "&res=" + displaySize();
		InputStream is = fetch(urlAvatar);
		Drawable drawable = Drawable.createFromStream(is, "src");
		this.avatar = drawable;
	}

	protected InputStream fetch(String urlString) throws MalformedURLException,
			IOException {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpGet request = new HttpGet(urlString);
		HttpResponse response = httpClient.execute(request);
		return response.getEntity().getContent();
	}
	
	protected String displaySize(){
		DisplayMetrics dm = ApplicationContext.c.getResources().getDisplayMetrics();

		switch(dm.densityDpi){
		     case DisplayMetrics.DENSITY_LOW:
		                return "ldpi";
		     case DisplayMetrics.DENSITY_MEDIUM:
		    	 		return "mdpi";
		     case DisplayMetrics.DENSITY_HIGH:
		    	 		return "hdpi";
		}
		return "hdpi";
	}
	 /**
	  * Custom deserialization is needed.
	  */
		
	  private void readObject(
	    ObjectInputStream aStream
	  ) throws IOException, ClassNotFoundException {
	    //manually deserialize and init superclass
		this.avatar= ApplicationContext.c.getResources().getDrawable(R.drawable.empty_avatar);
	    this.description=(String)aStream.readObject();
	    this.title=(String)aStream.readObject();
	    this.id=(Long)aStream.readObject();
	  }

	  /**
	  * Custom serialization is needed.
	  */
	  private void writeObject(ObjectOutputStream aStream) throws IOException {
	    aStream.writeObject(this.description);
	    aStream.writeObject(this.title);
	    aStream.writeObject(this.id);
	  }
	
}
